package com.itqf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itqf.bean.DtsGoods;
import com.itqf.bean.DtsRole;
import com.itqf.dto.DtsSearchDto;
import com.itqf.mapper.DtsGoodsMapper;
import com.itqf.service.DtsGoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName: shop_manage
 *
 * @author: MaTianWei
 * time: 2021/7/17 11:41
 * description:
 */
@Service
public class DtsGoodsServiceImpl implements DtsGoodsService {
    @Autowired
    private DtsGoodsMapper dtsGoodsMapper;

    public static final Logger logger = LoggerFactory.getLogger(DtsRoleServiceImpl.class);

    @Override
    public Map findGoodsByPage(DtsSearchDto dto) {
        logger.info("[角色查询]业务开始调用获取数据为:{}",dto);
        //1·分页插件启动
        PageHelper.startPage(dto.getPage(),dto.getLimit());
        //2·数据库查询
        List<DtsGoods> goodsList = dtsGoodsMapper.queryGoodsByPage(dto);
        //3·pageInfo封装
        PageInfo<DtsGoods> pageInfo = new PageInfo<>(goodsList);
        //4·封装返回结果数据
        Map data = new HashMap();
        data.put("total",pageInfo.getTotal());
        data.put("items",pageInfo.getList());
        logger.info("[角色查询]业务调用完毕,获取数据为:{}",data);
        return data;
    }
    //商品删除
    @Override
    public int removeGoodsById(Integer id) {
        int rows = dtsGoodsMapper.deleteByPrimaryKey(id);
        return rows;
    }


    //判断商品名是否存在
    @Override
    public boolean checkGoodsName(String goodsName) {
        Long count = dtsGoodsMapper.queryCountByGoodsName(goodsName);
        logger.debug("[商品查询]username={}的账号已存在数量为:{}",goodsName,count);
        return count >= 1;
    }
    //添加到数据库
    @Override
    public DtsGoods saveGoods(DtsGoods goods) {
        //1·完善goods的数据 补全没有的数据
        goods.setAddTime(new Date());
        goods.setDeleted(false);
        //2·需要修改的数据
        logger.debug("[保存商品]业务内部获取用户的密码为:{}",goods.getName());
        //3·插入数据库
        int rows = dtsGoodsMapper.insertGoods(goods);
        //4·返回
        return goods;
    }

    @Override
    public int updateGoods(DtsGoods goods) {
        goods.setUpdateTime(new Date());
        int rows = dtsGoodsMapper.updateByPrimaryKeySelective(goods.getBrandId());
        logger.debug("[修改角色]业务修改影响数据的形象行数rows:{}",rows);
        return rows;
    }
}
